import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from '@src/store/base'
import router from '@src/router'
import { getActivityList, getActivityListIds } from '@src/api/marketing'
import { getAccountRule } from '@src/api/member'
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ['themeColor', 'themeTitleColor']
    }
  ],
  properties: {
    modules: {
      type: Array,
      value: "",
    },
    login: {
      type: Boolean
    }
  },
  options: {
    multipleSlots: true,
    addGlobalClass: true
  },
  /**
   * 组件的初始数据
   */
  data: {
    activityList: [],
    pic: "",
    activityIds: [],
    enableActivityTypeTitle: false,
    obj: null
  },
  lifetimes: {
    attached: function () {
      console.log("modulesmodulesmodulesmodulesmodulesmodules", this.data.modules)
      let modulesData = this.data.modules[0];
      let pic = modulesData.pic;
      let params = JSON.parse(modulesData.biz.params);
      if (params) { this.setData({ activityIds: params.activityIds, enableActivityTypeTitle: params.enableActivityTypeTitle }) }
      this.setData({ pic })
      this.initData()
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    rechargeTap() {
      router.push({ name: 'member/balanceRecharge' })
    },
    initData() {
      let resApi = null;
      let activityIds = null
      if (this.data.activityIds.length > 0) {
        this.getActivityListIdsFunc()
      } else { this.getActivityListFunc() }
    },
    handleFunc(activityList) {
      let obj = {};
      activityList.forEach((item, index) => {
        //判断对象中是否已经存在了type,如果存在就往里面推数据。
        if (item.activityType in obj) {
          obj[item.activityType].data.push(item)
          return;
        }
        obj[item.activityType] = {
          theme: item.theme,
          type: item.activityType,
          data: [item]
        }
      })
      this.setData({ obj })
    },
    getActivityListIdsFunc() {
      getActivityListIds(this.data.activityIds).then(res => {
        if (res.code == 0) {
          // let activityList = res.data.filter(value => { return value.status == 1 });
          let activityList = res.data
          if (this.data.enableActivityTypeTitle) {
            this.handleFunc(activityList)
          } else {
            this.setData({ activityList })
          }
        }
      })
    },
    getActivityListFunc() {
      getActivityList().then(res => {
        if (res.code == 0) {
          let activityList = res.data
          if (this.data.enableActivityTypeTitle) {
            this.handleFunc(activityList)
          } else {
            this.setData({ activityList })
          }
        }
      })
    },
    handleActivityDetail(e) {
      if (!this.data.login) { this.triggerEvent('login'); return; }
      let activityCode = e.currentTarget.dataset.item.activityCode;
      let urlParams = { activityCode: activityCode }
      router.push({ name: 'member/consumeActivityDetail', params: urlParams })
    }
  }
})
